package de.koller.oceanFramework.thread.buffer;

import de.koller.oceanFramework.OceanException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class BufferReceiver extends Thread {
	private final Buffer<Object> buffer;

	private final Socket socket;
	private ObjectInputStream in;

	public BufferReceiver( Socket socket, Buffer<Object> buffer ) {
		this.buffer = buffer;
		this.socket = socket;
		this.setDaemon( true );
	}

	@Override
	public void run() {
		
		while( true ) {
			try {
				receiveAll();
			} catch( Exception ex ) {
				throw new OceanException( ex );
			}
		}
	}
	
	private void receiveAll() throws Exception {
		if( in == null )
			in = new ObjectInputStream( socket.getInputStream() );
		
		Object obj = in.readObject();
		buffer.push( obj );
	}
	
}
